<?php
namespace Manage\Controller;

/**
 * 商品分类
 * Class GoodsController
 * @package Manage\Controller
 */
class GoodsCatController extends BaseController
{

    /**
     * 商品列表
     */
    public function index()
    {
        $goods_cat = M('b2c_goods_cat');

        $parent_list = $goods_cat->where(['parent_id' => '0'])->select();
        $second_list = $goods_cat->where(['parent_id' => ['NEQ', '0']])->select();

        $this->assign('parent_list', $parent_list);
        $this->assign('second_list', $second_list);
        $this->display(); // 输出模板
    }

    /**
     * 添加商品分类
     */
    public function add()
    {

        $goods_cat = M('b2c_goods_cat');

        if (IS_POST) {

            $level = I('level');
            $parent_id = I('parent_id');
            $cat_name = I('cat_name');

            if ($level == 1) {
                $data['parent_id'] = '0';
                $data['cat_name'] = $cat_name;
            } elseif ($level == 2 && !empty($parent_id)) {
                $data['parent_id'] = $parent_id;
                $data['cat_name'] = $cat_name;
            } else {
                $this->error('请认真填写信息');
                exit;
            }
            $goods_cat_add = $goods_cat->add($data);

            if ($goods_cat_add) {
                $this->success('添加成功', U('GoodsCat/index'));
                exit;
            }
            $this->error('请认真填写信息');
            exit;
        } else {
            $parent_list = $goods_cat->where(['parent_id' => '0'])->select();
            $this->assign('parent_list', $parent_list);
            $this->display();
        }
    }


    /**
     * 修改商品分类
     */
    public function update()
    {

        $goods_cat = M('b2c_goods_cat');
        $cat_id = I('cat_id');

        if (empty($cat_id)) {
            $this->error('请认真填写信息1');
            exit;
        }

        if (IS_POST) {

            $level = I('level');
            $parent_id = I('parent_id');
            $cat_name = I('cat_name');

            if ($level == 1) {
                $data['parent_id'] = '0';
                $data['cat_name'] = $cat_name;
            } elseif ($level == 2 && !empty($parent_id)) {
                $data['parent_id'] = $parent_id;
                $data['cat_name'] = $cat_name;
            } else {
                $this->error('请认真填写信息');
                exit;
            }
            $goods_cat_add = $goods_cat->where(['cat_id' => $cat_id])->save($data);

            if ($goods_cat_add) {
                $this->success('修改成功', U('GoodsCat/index'));
                exit;
            }
            $this->error('请认真填写信息');
            exit;
        } else {
            $parent_list = $goods_cat->where(['parent_id' => '0'])->select();
            $this->assign('parent_list', $parent_list);

            $cat = $goods_cat->where(['cat_id' => $cat_id])->find();
            $this->assign('cat', $cat);
            $this->display();
        }
    }

    /**
     * 删除
     */
    public function delete()
    {
        $goods_cat = M('b2c_goods_cat');
        $cat_id = I('cat_id');

        if (empty($cat_id)) {
            $this->error('信息错误');
            exit;
        }
        $cat = $goods_cat->where(['cat_id' => $cat_id])->find();
        if ($cat['parent_id'] == '0') { //表示父级分类
            $second_cat = $goods_cat->where(['parent_id' => $cat['cat_id']])->find();
            if ($second_cat) {
                $this->error('不能删除，请先删除子分类');
            }
        }
        $delete = $goods_cat->delete($cat_id);
        if ($delete) {
            $this->success('删除成功', U('GoodsCat/index'));
        }else{
            $this->error('删除失败');
        }
    }

}